package util;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import com.example.administrator.text.WebViewActivity;
import com.example.administrator.text.WebshipActivity;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import view.diaLogView.ChoicePaymentDialog;

/**
 * 用于android 和js 的调用
 */
public class AndroidJSInterface {
	private Activity mActivity;
	private boolean mState = true; //有时间后台会调用两次 防止这种情况
	public AndroidJSInterface(Activity activity){
		mActivity = activity;
	}
	
	@JavascriptInterface
    public void startFunction(String string) {

		LogUtil.eE("String",string+"...........mState...."+mState);
		if(mState){
			mState = false;
			ChoicePaymentDialog choicePaymentDialog = new ChoicePaymentDialog(mActivity,string);
			choicePaymentDialog.show();
			EventBus.getDefault().post("state");
			HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
				@Override
				public void run() {
					mState = true;
				}
			},1200);
		}
	}

	@JavascriptInterface
	public void startFunction1(String string) {
		LogUtil.eE("String",string+"...........mState...."+mState);
		if(mState){
			mState = false;
			try {
				JSONObject jsonObject = new JSONObject(string);
				String context = jsonObject.getString("keyword");
				if(context.equals("return")){
					Intent intent = new Intent();
					intent.setAction(WebViewActivity.mAction);
					intent.putExtra("pay","WXPAY");
					mActivity.sendBroadcast(intent);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			HomeUtil.getHemeUtilNew().getHandlerNew().postDelayed(new Runnable() {
				@Override
				public void run() {
					mState = true;
				}
			},1200);
		}
	}
	
	@JavascriptInterface
	public String getInterface(){
		return "Dayment";
	}

	@JavascriptInterface
	public String getInterfaceString(){
		return "sendUrl";
	}


	@JavascriptInterface
	public void jsJavaScreen(){
		
		Intent intent = new Intent(WebshipActivity.mWEB);
		intent.putExtra("jsjava","huen");
		mActivity.sendBroadcast(intent);//发送一个广播
		/**
		 * 设置为横屏
		 */
		if(mActivity.getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
			mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制横屏
			mActivity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
		}
	}

	@JavascriptInterface
	public void jsJavaScreenVertical(){

		Intent intent = new Intent(WebshipActivity.mWEB);
		intent.putExtra("jsjava","shu");
		mActivity.sendBroadcast(intent);//发送一个广播
		/**
		 * 设置为竖屏
		 */
	    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制竖屏
	}

}
